package com.pengliu.config.logs;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSON;

@Aspect
@Component
public class ServiceLogAspect {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Pointcut("execution(* com.pengliu.service.*.impl.*.*(..))")
    public void serviceLog() {}
 
    @Around(value = "serviceLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    	String className = joinPoint.getTarget().getClass().getName();
    	String methodName = joinPoint.getSignature().getName();
        String classMethod = className + "." + methodName + "()";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    	logger.info("=================================================(Start -{}- Service)=================================================",methodName);
    	logger.info("◎调用业务:{}\t调用时间:{}",classMethod,sdf.format(new Date()));
        Object result = joinPoint.proceed(joinPoint.getArgs());
        logger.info("◎业务返回:{}\t耗时:{}毫秒",JSON.toJSONString(result));
        logger.info("===========================================(End -{}- Service)===========================================",methodName);
        return result;
    }
}
